Skip to main content

Simple Reports

KeyValuePairs

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata
var data = DemoKeyValuePairData.GetPair();
var metadata = DemoKeyValuePairData.GetDemoDictMetadata();

// Set ExportOption
var exportOption = new SingleDataSetOptions() { Metadata = metadata, };

// Call export method
Stream stream = exportManager.Export(ExportType.Excel, data, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}
}

// Sample Data Model
internal class DemoKeyValuePairData
{
// Method to create metadata
internal static List<PropertyMetadata> GetDemoDictMetadata()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Id",
DisplayName = "Id",
DisplayOrder = 1,
DataType = DataType.Number,
DisplayType = DisplayType.Integer
},
new PropertyMetadata()
{
Code = "Title",
DisplayName = "Title",
DisplayOrder = 2,
DataType = DataType.String,
DisplayType = DisplayType.URL
},
new PropertyMetadata()
{
Code = "Assignees",
DisplayName = "Assignees",
DisplayOrder = 3,
DataType = DataType.String,
DisplayType = DisplayType.S
},
};
return metadata;
}

// Method to create data as key-value pairs
internal static List<List<KeyValuePair<string, object>>> GetPair()
{
List<List<KeyValuePair<string, object>>> keyValuePairs = new();
for (int i = 0; i < 50; i++)
{
List<KeyValuePair<string, object>> dict1 = new();
dict1.Add(new KeyValuePair<string, object>("Id", 1));
dict1.Add(
new KeyValuePair<string, object>(
"Title",
"Add Key record in Dto | https://github.com/surya-soft/Surya.AbTemplate-POC/issues/89"
)
);
dict1.Add(new KeyValuePair<string, object>("Assignees", "Akash"));
keyValuePairs.Add(dict1);

List<KeyValuePair<string, object>> dict2 = new();
dict2.Add(new KeyValuePair<string, object>("Id", 2));
dict2.Add(
new KeyValuePair<string, object>(
"Title",
"CORS configuration in trial api | https://github.com/surya-soft/Surya.AbTemplate-POC/issues/117"
)
);
dict2.Add(new KeyValuePair<string, object>("Assignees", "Akash"));
keyValuePairs.Add(dict2);
}
return keyValuePairs;
}
}

DataTable

using System.Data;
using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata
var data = DemoDataTable.GetDataTable();
var metadata = DemoDataTable.GetDataTableMetadata();

// Set exportOption
var exportOption = new SingleDataSetOptions() { Metadata = metadata, };

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, data, exportOption, null);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoDataTable
{
// Method to create DataTable
internal static DataTable GetDataTable()
{
DataTable table = new() { TableName = "Demo Table" };
// Adding Columns
table.Columns.Add("Id", typeof(int));
table.Columns.Add("First Name", typeof(string));
table.Columns.Add("Last Name", typeof(string));
table.Columns.Add("DateOfBirth", typeof(DateTime));
table.Columns.Add("IsEmployee", typeof(bool));

// Adding Rows
table.Rows.Add(1, "Akash", "Chauhan", "8/5/2022", true);
table.Rows.Add(2, "Aman", "Gupta", "8/5/2022", true);
table.Rows.Add(3, "Dhruv", "Rathi", "8/5/2022", true);
table.Rows.Add(4, "Megha", "Tomar", "8/5/2022", true);
table.Rows.Add(5, "Vijay", "Gupta", "8/5/2022", true);
return table;
}

// Method to create metadata
internal static List<PropertyMetadata> GetDataTableMetadata()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Id",
DisplayName = "Id",
DisplayOrder = 1,
DataType = DataType.Number,
DisplayType = DisplayType.Integer
},
new PropertyMetadata()
{
Code = "First Name",
DisplayName = "First Name",
DisplayOrder = 2,
DataType = DataType.String,
DisplayType = DisplayType.S
},
new PropertyMetadata()
{
Code = "Last Name",
DisplayName = "Last Name",
DisplayOrder = 3,
DataType = DataType.String,
DisplayType = DisplayType.S
},
new PropertyMetadata()
{
Code = "DateOfBirth",
DisplayName = "Date",
DisplayOrder = 4,
DataType = DataType.DateTime,
DisplayType = DisplayType.DateTime
},
};
return metadata;
}
}
}

List of Objects

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata
var data = GetDemoData();
var metadata = GetDemoMetaData();

// Set exportOption
var exportOption = new SingleDataSetOptions() { Metadata = metadata, };

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, data, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoData
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
}

// Method to create Sample Data Instances
internal static List<DemoData> GetDemoData()
{
List<DemoData> data =
new()
{
new DemoData { Name = "Deposit", AccountBalance = 1540.457476 },
new DemoData { Name = "Current", AccountBalance = 100 },
new DemoData { Name = "Borrowing", AccountBalance = 100 },
new DemoData { Name = "Other", AccountBalance = 100 },
};
return data;
}

// Method to create Metadata
internal static List<PropertyMetadata> GetDemoMetaData()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.XXXL
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
};
return metadata;
}
}

ReportDataSet

ReportDataSet is passed through a List<DataSetBase>.
DataSetBase is the base class for all types of datasets.

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata
var data = GetDemoData();
var metadata = GetDemoMetaData();

/* NOTE:
* DatasetBase is the base class for all Dataset types.
* List<DatasetBase> is used to pass one or more datasets.
*/
//Create DatasetBase List with ReportDataSet
var datasetBaseList = new List<DataSetBase>()
{
new ReportDataSet(data)
{
Metadata = metadata, // optional
}
};

/* NOTE:
* When passing Datasets, an instance of ExportOptions is required.
* (NOT SingleDataSetOptions - it's properties are already present in Datasets)
*/
// Set exportOption
var exportOption = new ExportOptions();

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, datasetBaseList, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoData
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
}

// Method to create Sample Data Instances
internal static List<DemoData> GetDemoData()
{
List<DemoData> data =
new()
{
new DemoData { Name = "Deposit", AccountBalance = 1540.457476 },
new DemoData { Name = "Current", AccountBalance = 100 },
new DemoData { Name = "Borrowing", AccountBalance = 100 },
new DemoData { Name = "Other", AccountBalance = 100 },
};
return data;
}

// Method to create Metadata
internal static List<PropertyMetadata> GetDemoMetaData()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.XXXL
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
};
return metadata;
}
}

Parameter Data

Parameter Data is a Dictionary<string, string>. It is passed using ParamSectionDataset where it's data field is assigned with the Dictionary of Parameter Data.

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// set filePath
string filePath = @"..\OutputFile.xlsx";
// get data for parameters
var data = GetParamSecData();

// create ParamSectionDataset
var paramSecDataSet = new ParamSectionDataSet(data, ParamSectionOrientation.TwoColumns);

// add dataset to List<DataSetBase>
var listOfDatasetBase = new List<DataSetBase>() { paramSecDataSet };

// create ExportOption
var exportOption = new ExportOptions();

// call Export method
Stream stream = exportManager.Export(ExportType.Excel, listOfDatasetBase, exportOption);

// save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Method to create to Parameter data
internal static Dictionary<string, string> GetParamSecData()
{
Dictionary<string, string> dict =
new()
{
{ "Bank", "Surya" },
{ "As On", "31-05-2019" },
{ "Currency", "INR" },
{ "Denomination", "Units" },
{ "Model", "08' Mar'21 ALCO" },
{ "Plan", "Q4 Mar-21 and 3 Year Forecast" },
{ "Plan Entry Type", "Target EOP Balance" },
{ "Report For", "3 Months" },
{ "LLG Description", "Loans - CC" },
{ "Current Int. Rate % (D0)", "0.00" },
{ "Int. Calc. Basis", "30/360" }
};
return dict;
}
}

ReportDataSet with Parameter Data

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata for ReportDataSet
var data = GetDemoData();
var metadata = GetDemoMetaData();

// Get data for ParamSectionDataSet
var paramData = GetParamSecData();

/* NOTE:
* DatasetBase is the base class for all Dataset types.
* List<DatasetBase> is used to pass one or more datasets.
*/
//Create DatasetBase List with ReportDataSet and ParamSectionDataSet
var datasetBaseList = new List<DataSetBase>()
{
// create ReportDataSet
new ReportDataSet(data)
{
Metadata = metadata, // optional
},
//Create ParamSectionDataset
new ParamSectionDataSet(paramData)
};

/* NOTE:
* When passing Datasets, an instance of ExportOptions is required.
* (NOT SingleDataSetOptions - it's properties are already present in Datasets)
*/
// Set exportOption
var exportOption = new ExportOptions();

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, datasetBaseList, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoData
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
}

// Method to create Sample Data Instances
internal static List<DemoData> GetDemoData()
{
List<DemoData> data =
new()
{
new DemoData { Name = "Deposit", AccountBalance = 1540.457476 },
new DemoData { Name = "Current", AccountBalance = 100 },
new DemoData { Name = "Borrowing", AccountBalance = 100 },
new DemoData { Name = "Other", AccountBalance = 100 },
};
return data;
}

// Method to create Metadata for DemoData
internal static List<PropertyMetadata> GetDemoMetaData()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.XXXL
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
};
return metadata;
}

// Method to create to Parameter data
internal static Dictionary<string, string> GetParamSecData()
{
Dictionary<string, string> dict =
new()
{
{ "Bank", "Surya" },
{ "As On", "31-05-2019" },
{ "Currency", "INR" },
{ "Denomination", "Units" },
{ "Model", "08' Mar'21 ALCO" },
{ "Plan", "Q4 Mar-21 and 3 Year Forecast" },
{ "Plan Entry Type", "Target EOP Balance" },
{ "Report For", "3 Months" },
{ "LLG Description", "Loans - CC" },
{ "Current Int. Rate % (D0)", "0.00" },
{ "Int. Calc. Basis", "30/360" }
};
return dict;
}
}

Search Object Data

Search Object Data is a List<SearchDet>. It is passed using SearchObjectDataset.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Surya.Ab.Dto;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// set filePath
string filePath = @"..\OutputFile.xlsx";
// get data for search object dataset
var data = GetSearchDetails();

// create SearchObjectDataset
var searchObejctDataSet = new SearchObjectDataset(data)
{
Header = "Filters",
DataSetGap = 0,
Width = 300
};

// add dataset to List<DataSetBase>
var listOfDatasetBase = new List<DataSetBase>() { searchObejctDataSet };

// create ExportOption
var exportOption = new ExportOptions();

// call Export method
Stream stream = exportManager.Export(ExportType.Excel, listOfDatasetBase, exportOption);

// save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Method to create to Search Object data
internal static List<SearchDet> GetSearchDetails()
{
return new List<SearchDet>
{
new SearchDet
{
Fields = new()
{
new AbFieldInfo
{
FieldName = "First Name",
Value = "Deepak",
Operator = "eq"
},
new AbFieldInfo
{
FieldName = "Age",
Value = "20",
Operator = "gt"
},
new AbFieldInfo
{
FieldName = "DOB",
Value = new StringValues(new string[] { "2000-01-01", "2005-01-01" }),
Operator = "bt"
}
},
MatchAllFields = true
},
new SearchDet
{
Fields = new()
{
new AbFieldInfo
{
FieldName = "Role",
Value = new StringValues(new string[] { "Developer", "Tester" }),
Operator = "in"
},
new AbFieldInfo
{
FieldName = "Salary",
Value = "10,000",
Operator = "lt"
}
},
MatchAllFields = false
},
new SearchDet
{
Fields = new()
{
new AbFieldInfo
{
FieldName = "First Name",
Value = "Deepak",
Operator = "eq"
},
new AbFieldInfo
{
FieldName = "Age",
Value = "20",
Operator = "gt"
},
new AbFieldInfo
{
FieldName = "DOB",
Value = new StringValues(new string[] { "2000-01-01", "2005-01-01" }),
Operator = "bt"
}
},
MatchAllFields = true
},
};
}
}

ReportDataSet with Search Object Data

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Surya.Ab.Dto;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata for ReportDataSet
var data = GetDemoData();
var metadata = GetDemoMetaData();

// get data for search object dataset
var searchObjectData = GetSearchDetails();

/* NOTE:
* DatasetBase is the base class for all Dataset types.
* List<DatasetBase> is used to pass one or more datasets.
*/
//Create DatasetBase List with ReportDataSet and ParamSectionDataSet
var datasetBaseList = new List<DataSetBase>()
{
//Create SearchObjectDataset
new SearchObjectDataset(searchObjectData)
{
SheetName = "Sheet 1",
Header = "Filters",
DataSetGap = 0,
Width = 300
},
// create ReportDataSet
new ReportDataSet(data)
{
Metadata = metadata, // optional
SheetName = "Sheet 1"
},

};

/* NOTE:
* When passing Datasets, an instance of ExportOptions is required.
* (NOT SingleDataSetOptions - it's properties are already present in Datasets)
*/
// Set exportOption
var exportOption = new ExportOptions();

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, datasetBaseList, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoData
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
}

// Method to create Sample Data Instances
internal static List<DemoData> GetDemoData()
{
List<DemoData> data =
new()
{
new DemoData { Name = "Deposit", AccountBalance = 1540.457476 },
new DemoData { Name = "Current", AccountBalance = 100 },
new DemoData { Name = "Borrowing", AccountBalance = 100 },
new DemoData { Name = "Other", AccountBalance = 100 },
};
return data;
}

// Method to create Metadata for DemoData
internal static List<PropertyMetadata> GetDemoMetaData()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.XXXL
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
};
return metadata;
}


// Method to create to Search Object data
internal static List<SearchDet> GetSearchDetails()
{
return new List<SearchDet>
{
new SearchDet
{
Fields = new()
{
new AbFieldInfo
{
FieldName = "First Name",
Value = "Deepak",
Operator = "eq"
},
new AbFieldInfo
{
FieldName = "Age",
Value = "20",
Operator = "gt"
},
new AbFieldInfo
{
FieldName = "DOB",
Value = new StringValues(new string[] { "2000-01-01", "2005-01-01" }),
Operator = "bt"
}
},
MatchAllFields = true
},
new SearchDet
{
Fields = new()
{
new AbFieldInfo
{
FieldName = "Role",
Value = new StringValues(new string[] { "Developer", "Tester" }),
Operator = "in"
},
new AbFieldInfo
{
FieldName = "Salary",
Value = "10,000",
Operator = "lt"
}
},
MatchAllFields = false
},
new SearchDet
{
Fields = new()
{
new AbFieldInfo
{
FieldName = "First Name",
Value = "Deepak",
Operator = "eq"
},
new AbFieldInfo
{
FieldName = "Age",
Value = "20",
Operator = "gt"
},
new AbFieldInfo
{
FieldName = "DOB",
Value = new StringValues(new string[] { "2000-01-01", "2005-01-01" }),
Operator = "bt"
}
},
MatchAllFields = true
},
};
}
}

ImageDataset

Images can be passed using an ImageDataset. It can take a list of images.

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Drawing.Client.Enums;
using Surya.Ab.Drawing.Client.Models;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// set filePath
string filePath = @"..\OutputFile.xlsx";
// get images for export
var images = GetImageForChart();

// datasetOption
var exportOption = new SingleDataSetOptions()
{
ImageDatasetDisplay = ImageDatasetDisplay.SingleColumn,
FitToPage = true,
};

// call Export method
Stream stream = exportManager.Export(ExportType.Excel, images, exportOption);

// save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Method to get Images
internal static List<AbImage> GetImageForChart()
{
return new List<AbImage>()
{
new AbImage(File.ReadAllBytes("..\\chart.png"), AbImageType.Png)
{
Height = 500,
Width = 500,
}
};
}
}

ReportDataSet with Images

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Drawing.Client.Enums;
using Surya.Ab.Drawing.Client.Models;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata for ReportDataSet
var data = GetDemoData();
var metadata = GetDemoMetaData();

//ImageDataset
var imageDataset = GetDemoImageDataset();

/* NOTE:
* DatasetBase is the base class for all Dataset types.
* List<DatasetBase> is used to pass one or more datasets.
*/
//Create DatasetBase List with ReportDataSet and ImageDataSet
var datasetBaseList = new List<DataSetBase>()
{
imageDataset,
// create ReportDataSet
new ReportDataSet(data)
{
Metadata = metadata, // optional
SheetName = "Sheet 1"
},

};

/* NOTE:
* When passing Datasets, an instance of ExportOptions is required.
* (NOT SingleDataSetOptions - it's properties are already present in Datasets)
*/
// Set exportOption
var exportOption = new ExportOptions();

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, datasetBaseList, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoData
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
}

// Method to create Sample Data Instances
internal static List<DemoData> GetDemoData()
{
List<DemoData> data =
new()
{
new DemoData { Name = "Deposit", AccountBalance = 1540.457476 },
new DemoData { Name = "Current", AccountBalance = 100 },
new DemoData { Name = "Borrowing", AccountBalance = 100 },
new DemoData { Name = "Other", AccountBalance = 100 },
};
return data;
}

// Method to create Metadata for DemoData
internal static List<PropertyMetadata> GetDemoMetaData()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.XXXL
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
};
return metadata;
}

// method to create ImageDataset
internal static ImageDataset GetDemoImageDataset()
{
var images = new List<AbImage>
{
new AbImage(null, AbImageType.Png) { Height = 20, Width = 200 },
new AbImage(File.ReadAllBytes("..\\HDFC_logo.jpg"), AbImageType.Jpeg)
{
Height = 70,
Width = 200,
},
};

var imageDataset = new ImageDataset(images)
{
Display = ImageDatasetDisplay.SingleRow,
FitToPage = true
};

return imageDataset;
}
}

ReportDataSet with RichHeader

Sample Output: RichHeaderReport

Sample Program:

using Microsoft.Extensions.DependencyInjection;
using Surya.Ab.Export.Client.Enum;
using Surya.Ab.Export.Client.Interface;
using Surya.Ab.Export.Client.Models;
using Surya.Ab.Metadata.Enums;
using Surya.Ab.Metadata.Models;

namespace ExportExample;

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Set filePath
string filePath = @"..\OutputFile.xlsx";

// Get data and metadata for ReportDataSet
var data = GetDemoData();
var metadata = GetDemoMetaData();

/* NOTE:
* DatasetBase is the base class for all Dataset types.
* List<DatasetBase> is used to pass one or more datasets.
*/
//Create DatasetBase List with ReportDataSet
var datasetBaseList = new List<DataSetBase>()
{
// create ReportDataSet
new ReportDataSet(data)
{
SheetName = "Sheet 1",
Metadata = metadata, // optional
TableHeader = GetRichHeader()
},
};

/* NOTE:
* When passing Datasets, an instance of ExportOptions is required.
* (NOT SingleDataSetOptions - it's properties are already present in Datasets)
*/
// Set exportOption
var exportOption = new ExportOptions();

// Call Export method
Stream stream = exportManager.Export(ExportType.Excel, datasetBaseList, exportOption);

// Save stream to file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// Sample Data Model
internal class DemoData
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
public int B2 { get; set; }
public decimal B3 { get; set; }
public DateTime B4 { get; set; }
}

// Method to create Sample Data Instances
internal static List<DemoData> GetDemoData()
{
List<DemoData> data =
[
new DemoData
{
Name = null,
AccountBalance = 100.1245635,
B2 = 200,
B3 = 300,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Deposit",
AccountBalance = 1540.457476,
B2 = 200,
B3 = 45.34M,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Current",
AccountBalance = 100,
B2 = 200,
B3 = 345.67895M,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Borrowing",
AccountBalance = 100,
B2 = 200,
B3 = 300,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Other",
AccountBalance = 100,
B2 = 200,
B3 = 300,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Bills",
AccountBalance = 100,
B2 = 200,
B3 = 300,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Provisions",
AccountBalance = 100,
B2 = 200,
B3 = 300,
B4 = new DateTime(2013, 12, 1)
},
new DemoData
{
Name = "Credit",
AccountBalance = 100,
B2 = 200,
B3 = 300,
B4 = new DateTime(2013, 12, 1)
},
];

return data;
}

// Method to create Metadata for DemoData
internal static List<PropertyMetadata> GetDemoMetaData()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.L
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
new PropertyMetadata()
{
Code = "B2",
DisplayName = "1D - 2D",
DisplayOrder = 3,
DataType = DataType.Number,
DisplayType = DisplayType.Integer
},
new PropertyMetadata()
{
Code = "B3",
DisplayName = "3D - 4D",
DisplayOrder = 4,
DataType = DataType.Number,
DisplayType = DisplayType.Integer
},
new PropertyMetadata()
{
Code = "B4",
DisplayName = "5D - 7D",
DisplayOrder = 5,
DataType = DataType.DateTime,
DisplayType = DisplayType.ShortMonth
},
};
return metadata;
}

// Method to create RichHeader
internal static List<AbHeader> GetRichHeader()
{
HeaderGroup header1 = new HeaderGroup() { Name = "Header 1" };

HeaderGroup subHeader1 = new HeaderGroup()
{
Name = "Header 2",
SubHeaderGroup = new HeaderGroup() { Name = "SubHeader 1" }
};
HeaderGroup subHeader2 = new HeaderGroup()
{
Name = "Header 2",
SubHeaderGroup = new HeaderGroup() { Name = "SubHeader 2" }
};

List<AbHeader> headers =
new()
{
new() { Code = "Name", HeaderGroup = header1 },
new() { Code = "AccountBalance", HeaderGroup = header1 },
new() { Code = "B2", HeaderGroup = subHeader1 },
new() { Code = "B3", HeaderGroup = subHeader1 },
new() { Code = "B4", HeaderGroup = subHeader2 },
};
return headers;
}
}

Edit on GitHub